package com.itxg.booksmanage.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itxg.booksmanage.exception.BizException;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginFilter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        boolean result = false;
        try {
            // 如果 session 中存有 login 字段，说明已登录
            result = (boolean) request.getSession().getAttribute("login");
        } catch (Exception e) {
            // 未登录，抛出空指针异常
            throw new BizException("-1", "您还未登录，请先完成登录哦！");
        }
        if (result) {
            return true; // 已登录，放行
        }
        return false; // 未登录，拦截
    }

}

